package helperClassSearch;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class CreateUtils {

	public static void CreateUtilsClass(int[] sheetNumbers, String fileName, String packageName, String className) {

		File replacedFile = new File(fileName);
		replacedFile.delete();

		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			FileWriter fstream = new FileWriter(fileName);
			BufferedWriter textFile = new BufferedWriter(fstream);

			
			textFile.write("package com.cyclingthealps." + packageName + ";\n");
			textFile.write("\n");
			textFile.write("import com.cyclingthealps.client.dialogs.GoogleEarth;\n");
			textFile.write("import com.cyclingthealps.client.dialogs.Profile;\n");
			textFile.write("import com.cyclingthealps.client.planned.MyMessagesPlanned;\n");
			textFile.write("import com.cyclingthealps.client.protour.MyMessagesProTour;\n");
			textFile.write("import com.cyclingthealps.client.climbs.MyMessagesClimbs;\n");
			textFile.write("import com.google.gwt.core.client.GWT;\n");
			textFile.write("import com.google.gwt.maps.client.geom.LatLng;\n");
			textFile.write("import com.google.gwt.user.client.History;\n");
			textFile.write("import com.google.gwt.user.client.Window;\n");
			textFile.write("\n");
			textFile.write("			public class Utils {\n");
			textFile.write("public static MyMessages res = (MyMessages) GWT.create(MyMessages.class);\n");
			textFile.write("public static MyMessagesPlanned resPlanned = (MyMessagesPlanned) GWT.create(MyMessagesPlanned.class);\n");
			textFile.write("public static MyMessagesProTour resProTour = (MyMessagesProTour) GWT.create(MyMessagesProTour.class);\n");
			textFile.write("public static MyMessagesClimbs resClimbs = (MyMessagesClimbs) GWT.create(MyMessagesClimbs.class);\n");
			textFile.write("\n");
			textFile.write("				public static void linksCTA(String historyToken) {\n");
			
			
			for (int d = 0; d < sheetNumbers.length; d++) {
				Sheet sheet = workbook.getSheet(sheetNumbers[d]);
				Cell[] count = sheet.getColumn(0);
				String resources;
				if (sheetNumbers[d] == 0 || sheetNumbers[d] == 1 || sheetNumbers[d] == 2 || sheetNumbers[d] == 3) {
					resources = "res.";
				}
				else{
					if (sheetNumbers[d] == 11) {
						resources = "resProTour.";
					}
					else {
						if (sheetNumbers[d] == 17) {
							resources = "resClimbs.";
						}
						else {
							resources = "resPlanned.";	
						}
					}

				}
//				textFile.write("\n");
//				textFile.write("//Zoom in \n");
//				for (int i = 1; i < count.length; i++) {
//					Cell passLinkCell = sheet.getCell(34, i);
//					String linkResource = passLinkCell.getContents();
//					System.out.println(linkResource);
//					textFile.write("if (historyToken.endsWith(" + resources + "");
//					textFile.write(linkResource + "()");
//					Cell passOracleCell = sheet.getCell(40, i);
//					String oracleResource = passOracleCell.getContents();
//					textFile.write(")||historyToken.endsWith(" + resources + "");
//					textFile.write(oracleResource + "()");
//					textFile.write(")) {\n");
//					textFile.write("LatLng point = LatLng.newInstance(");
//					Cell latlngCellX = sheet.getCell(1, i);
//					String latlngX = latlngCellX.getContents();
//					textFile.write(latlngX);
//					textFile.write(", ");
//					Cell latlngCellY = sheet.getCell(2, i);
//					String latlngY = latlngCellY.getContents();
//					textFile.write(latlngY);
//					textFile.write(");\n");
//					textFile.write("Map.ZoominDirect(point);\n");
//					textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "());\n");
//					textFile.write("}\n");
//
//				}
				
				textFile.write("\n");
				textFile.write("//Overview \n");
				for (int i = 1; i < count.length; i++) {
					
					Cell passLinkCell = sheet.getCell(34, i);
					String linkResource = passLinkCell.getContents();
					System.out.println(linkResource);
					textFile.write("if (historyToken.endsWith(" + resources + "");
					textFile.write(linkResource + "()+ \"-\" + res.OverviewLink()");
					Cell passOracleCell = sheet.getCell(40, i);
					String oracleResource = passOracleCell.getContents();
					textFile.write(")||historyToken.endsWith(" + resources + "");
					textFile.write(oracleResource + "() + \" \" + res.OverviewOracle()");
					textFile.write(")) {\n");
					textFile.write("LatLng point = LatLng.newInstance(");
					Cell latlngCellX = sheet.getCell(1, i);
					String latlngX = latlngCellX.getContents();
					textFile.write(latlngX);
					textFile.write(", ");
					Cell latlngCellY = sheet.getCell(2, i);
					String latlngY = latlngCellY.getContents();
					textFile.write(latlngY);
					textFile.write(");\n");
					textFile.write("Map.ZoominDirect(point);\n");
					textFile.write("EntryDialog.hide();\n");
					
					Cell passNameCell = sheet.getCell(3, i);
					String passName = passNameCell.getContents();
					
					Cell siteNameCell = sheet.getCell(9, i);
					String siteName = siteNameCell.getContents();
					
					Cell passDescriptionCell = sheet.getCell(10, i);
					String passDescription = passDescriptionCell.getContents();
					
					Cell passRouteCell = sheet.getCell(16, i);
					String passRoute = passRouteCell.getContents();

					Cell passVideoCell = sheet.getCell(29, i);
					String passVideo = passVideoCell.getContents();

					Cell passSlideshowCell = sheet.getCell(30, i);
					String passSlideshow = passSlideshowCell.getContents();

					Cell passWebelementCell = sheet.getCell(17, i);
					String passWebelement = passWebelementCell.getContents();

					Cell BikemapCell = sheet.getCell(23, i);
					String bikemap = BikemapCell.getContents();

					Cell streetviewCell = sheet.getCell(46, i);
					String streetview = streetviewCell.getContents();

					Cell motorbikeCell = sheet.getCell(25, i);
					String motorbike = motorbikeCell.getContents();

					Cell dCell = sheet.getCell(26, i);
					String ds = dCell.getContents();

					Cell climbbybikeCell = sheet.getCell(27, i);
					String climbbybike = climbbybikeCell.getContents();

					Cell qualdichCell = sheet.getCell(28, i);
					String qualdich = qualdichCell.getContents();

					Cell cyclingchallengeCell = sheet.getCell(31, i);
					String cyclingchallenge = cyclingchallengeCell.getContents();

					
					textFile.write("PassDialog.setOptions(point, "+resources + passName + "(), \"" + siteName + "\", "+ resources + passDescription + "(),\"" + passRoute + "\", \""+ passVideo +"\", \""+ passSlideshow +"\", "+ resources + passWebelement + "(), \"" + bikemap + "\", \""+ streetview +"\", \"" + motorbike + "\", " + ds +", \"" + climbbybike +"\", \""+ qualdich +"\", \"" + cyclingchallenge +"\");\n");
					
					
//					textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "() + \"-\" + res.OverviewLink());\n");
					textFile.write("}\n");

				}
				
				
				textFile.write("\n");
				textFile.write("//3D TOUR \n");
				// create 3D tour
				for (int j = 1; j < count.length; j++) {
					Cell passLinkCell = sheet.getCell(34, j);
					String linkResource = passLinkCell.getContents();
					System.out.println(linkResource);
					textFile.write("if (historyToken.endsWith(" + resources + "");
					textFile.write(linkResource + "() + \"-tour\"");
					Cell passOracleCell = sheet.getCell(40, j);
					String oracleResource = passOracleCell.getContents();
					textFile.write(")||historyToken.endsWith(" + resources + "");
					textFile.write(oracleResource + "() + \" 3D Tour\"");
					textFile.write(")) {\n");
					textFile.write("LatLng point = LatLng.newInstance(");
					Cell latlngCellX = sheet.getCell(1, j);
					String latlngX = latlngCellX.getContents();
					textFile.write(latlngX);
					textFile.write(", ");
					Cell latlngCellY = sheet.getCell(2, j);
					String latlngY = latlngCellY.getContents();
					textFile.write(latlngY);
					textFile.write(");\n");
					textFile.write("Map.ZoominDirect(point);\n");
					textFile.write("EntryDialog.hide();\n");
					Cell siteNameCell = sheet.getCell(9, j);
					String siteName = siteNameCell.getContents();
					textFile.write("GoogleEarth.setEarth(\"" + siteName + "\");\n");
					textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "() + \"-tour\" );\n");
					textFile.write("}\n");

				}

				if (sheetNumbers[d] == 0 || sheetNumbers[d] == 1 || sheetNumbers[d] == 2 || sheetNumbers[d] == 3) {
					textFile.write("\n");
					textFile.write("//Video \n");
					// create video
					for (int j = 1; j < count.length; j++) {
						Cell passLinkCell = sheet.getCell(34, j);
						String linkResource = passLinkCell.getContents();
						System.out.println(linkResource);
						textFile.write("if (historyToken.endsWith(" + resources + "");
						textFile.write(linkResource + "() + \"-video\"");
						Cell passOracleCell = sheet.getCell(40, j);
						String oracleResource = passOracleCell.getContents();
						textFile.write(")||historyToken.endsWith(" + resources + "");
						textFile.write(oracleResource + "() + \" Video\"");
						textFile.write(")) {\n");
						textFile.write("LatLng point = LatLng.newInstance(");
						Cell latlngCellX = sheet.getCell(1, j);
						String latlngX = latlngCellX.getContents();
						textFile.write(latlngX);
						textFile.write(", ");
						Cell latlngCellY = sheet.getCell(2, j);
						String latlngY = latlngCellY.getContents();
						textFile.write(latlngY);
						textFile.write(");\n");
						textFile.write("Map.ZoominDirect(point);\n");
						textFile.write("EntryDialog.hide();\n");
						Cell videoCell = sheet.getCell(29, j);
						String video = videoCell.getContents();
						textFile.write("Video.setVideo(\"" + video + "\");\n");
						textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "() + \"-video\" );\n");
						textFile.write("}\n");
					}

				}

				if (sheetNumbers[d] == 0 || sheetNumbers[d] == 1 || sheetNumbers[d] == 2 || sheetNumbers[d] == 3) {
					textFile.write("\n");
					textFile.write("//Slideshow \n");
					// create slideshow
					for (int j = 1; j < count.length; j++) {
						Cell passLinkCell = sheet.getCell(34, j);
						String linkResource = passLinkCell.getContents();
						System.out.println(linkResource);
						textFile.write("if (historyToken.endsWith(" + resources + "");
						textFile.write(linkResource + "() + \"-slideshow\"");
						Cell passOracleCell = sheet.getCell(40, j);
						String oracleResource = passOracleCell.getContents();
						textFile.write(")||historyToken.endsWith(" + resources + "");
						textFile.write(oracleResource + "() + \" Slideshow\"");
						textFile.write(")) {\n");
						textFile.write("LatLng point = LatLng.newInstance(");
						Cell latlngCellX = sheet.getCell(1, j);
						String latlngX = latlngCellX.getContents();
						textFile.write(latlngX);
						textFile.write(", ");
						Cell latlngCellY = sheet.getCell(2, j);
						String latlngY = latlngCellY.getContents();
						textFile.write(latlngY);
						textFile.write(");\n");
						textFile.write("Map.ZoominDirect(point);\n");
						textFile.write("EntryDialog.hide();\n");
						Cell slidesShow = sheet.getCell(30, j);
						String slideshow = slidesShow.getContents();
						textFile.write("Pictures.setPictures(\"" + slideshow + "\");\n");
						textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "() + \"-slideshow\" );\n");
						textFile.write("}\n");
					}
				}

//				textFile.write("\n");
//				textFile.write("//Game \n");
//				// create Game
//				for (int j = 1; j < count.length; j++) {
//					Cell siteNameCell = sheet.getCell(9, j);
//					String siteName = siteNameCell.getContents();
//					System.out.println(siteName);
//					textFile.write("if (historyToken.endsWith(\"http://www.cyclingthealps.com/game/" + siteName + ".html\"");
//					Cell passOracleCell = sheet.getCell(40, j);
//					String oracleResource = passOracleCell.getContents();
//					textFile.write(")||historyToken.endsWith(" + resources + "");
//					textFile.write(oracleResource + "() + \" Game\"");
//					textFile.write(")) {\n");
//
//					textFile.write("if (Window.Navigator.getAppName().endsWith(\"Microsoft Internet Explorer\")) {\n");
//					textFile.write("Window.open(\"http://www.cyclingthealps.com/game/" + siteName + ".html\", \"game\", \"\");\n");
//					textFile.write("}\n");
//					textFile.write("else {\n");
//					textFile.write("Window.Location.assign(\"game/" + siteName + ".html\");\n");
//					textFile.write("}\n");
//					textFile.write("}\n");
//
//				}
//				textFile.write("\n");
//				textFile.write("//Profile \n");
//				// create profile
//				for (int j = 1; j < count.length; j++) {
//					Cell passLinkCell = sheet.getCell(34, j);
//					String linkResource = passLinkCell.getContents();
//					System.out.println(linkResource);
//					textFile.write("if (historyToken.endsWith(" + resources + "");
//					textFile.write(linkResource + "() + \"-profile\"");
//					Cell passOracleCell = sheet.getCell(40, j);
//					String oracleResource = passOracleCell.getContents();
//					textFile.write(")||historyToken.endsWith(" + resources + "");
//					textFile.write(oracleResource + "() + \" Profile\"");
//					textFile.write(")) {\n");
//					textFile.write("LatLng point = LatLng.newInstance(");
//					Cell latlngCellX = sheet.getCell(1, j);
//					String latlngX = latlngCellX.getContents();
//					textFile.write(latlngX);
//					textFile.write(", ");
//					Cell latlngCellY = sheet.getCell(2, j);
//					String latlngY = latlngCellY.getContents();
//					textFile.write(latlngY);
//					textFile.write(");\n");
//					textFile.write("Map.ZoominDirect(point);\n");
//					textFile.write("EntryDialog.hide();\n");
//					Cell siteNameCell = sheet.getCell(9, j);
//					String siteName = siteNameCell.getContents();
//					textFile.write("Profile.setProfile(\"" + siteName + "\");\n");
//					textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "() + \"-profile\" );\n");
//					textFile.write("}\n");
//
//				}
//				textFile.write("\n");
//				textFile.write("//Streetview \n");
//				// create streetview
//				for (int j = 1; j < count.length; j++) {
//					Cell streetviewTour = sheet.getCell(56, j);
//					String streetview = streetviewTour.getContents();
//					if (!streetview.endsWith("NOP")) {
//						Cell passLinkCell = sheet.getCell(34, j);
//						String linkResource = passLinkCell.getContents();
//						System.out.println(linkResource);
//						textFile.write("if (historyToken.endsWith(" + resources + "");
//						textFile.write(linkResource + "() + \"-streetview\"");
//						Cell passOracleCell = sheet.getCell(40, j);
//						String oracleResource = passOracleCell.getContents();
//						textFile.write(")||historyToken.endsWith(" + resources + "");
//						textFile.write(oracleResource + "() + \" Streetview\"");
//						textFile.write(")) {\n");
//						textFile.write("LatLng point = LatLng.newInstance(");
//						Cell latlngCellX = sheet.getCell(1, j);
//						String latlngX = latlngCellX.getContents();
//						textFile.write(latlngX);
//						textFile.write(", ");
//						Cell latlngCellY = sheet.getCell(2, j);
//						String latlngY = latlngCellY.getContents();
//						textFile.write(latlngY);
//						textFile.write(");\n");
//						textFile.write("Map.ZoominDirect(point);\n");
//						textFile.write("EntryDialog.hide();\n");
//						Cell siteNameCell = sheet.getCell(9, j);
//						String siteName = siteNameCell.getContents();
//						textFile.write("Streetview.setStreetview(\"" + streetview + "\", \""+ siteName + "\");\n");
//						textFile.write("History.newItem(\"!\" + " + resources + "" + linkResource + "() + \"-streetview\" );\n");
//						textFile.write("}\n");
//
//					}
//
//				}
//				textFile.write("\n");
//				textFile.write("//Explore \n");
//				// create Explore
//				for (int j = 1; j < count.length; j++) {
//					Cell siteNameCell = sheet.getCell(9, j);
//					String siteName = siteNameCell.getContents();
//					System.out.println(siteName);
//					textFile.write("if (historyToken.endsWith(\"http://www.cyclingthealps.com/explore/" + siteName + ".html\"");
//					Cell passOracleCell = sheet.getCell(40, j);
//					String oracleResource = passOracleCell.getContents();
//					textFile.write(")||historyToken.endsWith(" + resources + "");
//					textFile.write(oracleResource + "() + \" Explore\"");
//					textFile.write(")) {\n");
//
//					textFile.write("if (Window.Navigator.getAppName().endsWith(\"Microsoft Internet Explorer\")) {\n");
//					textFile.write("Window.open(\"http://www.cyclingthealps.com/explore/" + siteName + ".html\", \"explore\", \"\");\n");
//					textFile.write("}\n");
//					textFile.write("else {\n");
//					textFile.write("Window.Location.assign(\"explore/" + siteName + ".html\");\n");
//					textFile.write("}\n");
//					textFile.write("}\n");
//					
//
//				}

			}
			textFile.write("}\n");
			textFile.write("}\n");
			textFile.write("\n");
			textFile.close();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
